package com.study.servlet.servletcontext.resource;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* @Name: ReadResourceFileDemo01
* @Description: 使用ServletContext.getRealPath()读取应用资源文件方式
* 方式：
* 	获取当前应用程序路径，用"/"代表当前应用路径
* 特点：可以加载所有目录下的文件，但只适用于当前web项目
* @Author: 李超	lc_jack@foxmail.com
* @CreateDate: 2017-3-1 上午9:30:05 
* @Version: V1.0
 */
public class ReadResourceFileDemo01 extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ServletContext context = this.getServletContext() ;
		String path = context.getRealPath("/db.properties") ;
//		String path = context.getRealPath("/WEB-INF/db.properties") ;
//		String path = context.getRealPath("/WEB-INF/classes/db.properties") ;
//		String path = context.getRealPath("/WEB-INF/classes/com/study/servlet/servletcontext/db.properties") ;
		System.out.println(path);
		Properties props = new Properties() ;
		props.load(new FileInputStream(path)) ;
		String DBDRIVER = props.getProperty("DBDRIVER") ;
		System.out.println(DBDRIVER);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}

}
